package com.szm.lnr.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by szm on 2018/7/27.
 */

public class MainLogicUtil {

    private static final long INTERVAL = 30;
    private static final String COLOR_WHITE = "#FFFFFF";
    private static final String COLOR_BLACK = "#333333";
    private static final String COLOR_RED = "#FF0000";
    private static final String COLOR_GREEN = "#FF00FF";
    private static final String COLOR_REMOVE = "#80FF00FF";
    private static final long CUR_INTERVAL = 510;//990

    private Random mRandom;
    private Thread mThread;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceView mSurfaceView;
    private Context mContext;
    private HolderCallBack mHolderCallBack;
    private Handler mHandler;
    private Canvas mCanvas;
    private Paint mPaint;
    /**
     * 活动图
     */
    private int[][] curShape;
    /**
     * 底图
     */
    private int[][] mArea;
    /**
     * 方格尺寸
     */
    private int sizeBox;
    /**
     * 绘制起始点
     */
    private int startX, startY;
    /**
     * 是否结束刷新
     */
    private boolean over;
    /**
     * 是否暂停刷新
     */
    private boolean puase;
    /**
     * 是否启动消除动画
     */
    private boolean remove;
    /**
     * 消除的行
     */
    private List<Integer> removeList;
    /**
     * 消除动画闪烁次数
     */
    private int removeCount;
    /**
     * 边距
     */
    private int mPadding;
    /**
     * 活动图形计时
     */
    private long shapeTime;
    /**
     * 活动图形下移时间间隔
     */
    private long curInterval;
    /**
     * 活动图形是否已到达底部
     */
    private boolean curShapeArrive;
    /**
     * 当前活动图形类型
     */
    private int curShapeType;
    /**
     * 当前活动图形形态
     */
    private int curShapeTransformation;
    /**
     * 活动图形是否快速下降
     */
    private boolean curShapeFast;
    /**
     * 消除积分
     */
    private int gameCredits;
    /**
     * 加速参数
     */
    private double speedRatio;

    private MainLogicUtil(){
        removeList = new ArrayList<>();
        curShape = new int[4][2];
        mArea = new int[][]{
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        };
    }

    private void setSurfaceView(SurfaceView surfaceView) {
        this.mSurfaceView = surfaceView;
        this.mSurfaceHolder = mSurfaceView.getHolder();
        init();
    }

    private void setCurInterval(long interval){
        this.curInterval = interval;
    }


    private void setPadding(int padding){
        this.mPadding = padding;
        sizeBox = Math.min((DeviceUtil.getScreenRealSize(mContext)[0] - 2 * mPadding) / 10, (DeviceUtil.getScreenRealSize(mContext)[1] - 2 * mPadding - 60) / 20);
        startX = (DeviceUtil.getScreenRealSize(mContext)[0] - sizeBox * 10) / 2;
        startY = (DeviceUtil.getScreenRealSize(mContext)[1] - sizeBox * 20) - mPadding;
    }

    private void setHandler(Handler handler){
        mHandler = handler;
    }

    private void init(){
        mRandom = new Random();
        mContext = mSurfaceView.getContext();
        mHolderCallBack = new HolderCallBack();
        mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
        mSurfaceView.setZOrderOnTop(true);
        mSurfaceHolder.addCallback(mHolderCallBack);
        mPaint = new Paint();
        speedRatio = 1f;
        curInterval = CUR_INTERVAL;
        sizeBox = Math.min((DeviceUtil.getScreenRealSize(mContext)[0] - 2 * mPadding) / 10, (DeviceUtil.getScreenRealSize(mContext)[1] - 2 * mPadding - 60) / 20);
        startX = (DeviceUtil.getScreenRealSize(mContext)[0] - sizeBox * 10) / 2;
        startY = (DeviceUtil.getScreenRealSize(mContext)[1] - sizeBox * 20) - mPadding;
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                createShape();
                while (!over) {
                    try {
                        if (puase){
                            Thread.sleep(INTERVAL);
                            continue;
                        }
                        if (mSurfaceView != null && mSurfaceHolder != null){
                            drawAre();
                        }
                        if (remove){
                            if (removeCount < 15){
                                Thread.sleep(INTERVAL);
                                removeCount += 1;
                                continue;
                            }
                            for (int i=0; i<mArea.length; i++){
                                if (removeList.contains(i)){
                                    if (i == 0){
                                        for (int j = 0; j<mArea[i].length; j++){
                                            mArea[i][j] = 0;
                                        }
                                    }else {
                                        for (int j=i; j>=0; j--){
                                            if (j == 0){
                                                for (int k = 0; k<mArea[j].length; k++){
                                                    mArea[j][k] = 0;
                                                }
                                            }else {
                                                for (int k = 0; k < mArea[j].length; k++) {
                                                    mArea[j][k] = mArea[j - 1][k];
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            remove = false;
                            removeCount = 0;
                        }else {
                            if (curShapeFast){
                                Thread.sleep(INTERVAL / 10);
                                shapeTime += INTERVAL * 5;
                            }else {
                                Thread.sleep((long)(speedRatio * INTERVAL));
                                shapeTime += INTERVAL;
                            }
                            if (!curShapeArrive) {
                                if (shapeTime >= curInterval) {
                                    shapeTime = 0;
                                    if (curShape[0][1] + 1 < mArea.length && curShape[1][1] + 1 < mArea.length &&
                                            curShape[2][1] + 1 < mArea.length && curShape[3][1] + 1 < mArea.length &&
                                            mArea[curShape[0][1] + 1][curShape[0][0]] == 0 && mArea[curShape[1][1] + 1][curShape[1][0]] == 0 &&
                                            mArea[curShape[2][1] + 1][curShape[2][0]] == 0 && mArea[curShape[3][1] + 1][curShape[3][0]] == 0) {
                                        curShape[0][1] += 1;
                                        curShape[1][1] += 1;
                                        curShape[2][1] += 1;
                                        curShape[3][1] += 1;
                                    } else {
                                        if (curShape[0][1] < 3 || curShape[1][1] < 3 || curShape[2][1] < 3 || curShape[3][1] < 3) {
                                            over = true;
                                        }
                                        mArea[curShape[0][1]][curShape[0][0]] = 1;
                                        mArea[curShape[1][1]][curShape[1][0]] = 1;
                                        mArea[curShape[2][1]][curShape[2][0]] = 1;
                                        mArea[curShape[3][1]][curShape[3][0]] = 1;
                                        curShapeArrive = true;
                                        curShapeFast = false;
                                        removeList.clear();
                                        for (int i=0; i<mArea.length; i++){
                                            int sum = 0;
                                            for (int j=0; j<mArea[i].length; j++){
                                                sum += mArea[i][j];
                                            }
                                            if (sum == mArea[i].length){
                                                removeList.add(i);
                                            }
                                        }
                                        if (removeList.size() > 0){
                                            gameCredits += (int)Math.pow(2, removeList.size());
                                            speedRatio = speedRatio * (Math.pow(0.9, gameCredits / 50));
                                            remove = true;
                                        }
                                    }
                                }
                            } else {
                                createShape();
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                mHandler.sendEmptyMessage(0);
            }
        });
    }

    private void drawAre(){
        try {
            mCanvas = mSurfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.parseColor(COLOR_BLACK));
            if (!curShapeArrive) {
                mPaint.reset();
                mPaint.setColor(Color.parseColor(COLOR_RED));
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setStrokeWidth(2);
                for (int i = 0; i < curShape.length; i++) {
                    if (curShape[i][1] >= 3){
                        int left = startX + curShape[i][0] * sizeBox;
                        int top = startY + (curShape[i][1] - 3) * sizeBox;
                        int right = left + sizeBox;
                        int bottom = top + sizeBox;
                        mCanvas.drawRect(left, top, right, bottom, mPaint);
                    }
                }
            }
            for (int i=3; i<mArea.length; i++){
                for (int j = 0; j < mArea[i].length; j++) {
                    if (remove){
                        if (mArea[i][j] == 1) {
                            if (removeList.contains(i) && removeCount % 5 < 2){
                                mPaint.reset();
                                mPaint.setColor(Color.parseColor(COLOR_REMOVE));
                                mPaint.setStyle(Paint.Style.FILL);
                                mPaint.setStrokeWidth(2);
                                int left = startX + j * sizeBox;
                                int top = startY + (i - 3) * sizeBox;
                                int right = left + sizeBox;
                                int bottom = top + sizeBox;
                                mCanvas.drawRect(left, top, right, bottom, mPaint);
                            }else {
                                mPaint.reset();
                                mPaint.setColor(Color.parseColor(COLOR_GREEN));
                                mPaint.setStyle(Paint.Style.FILL);
                                mPaint.setStrokeWidth(2);
                                int left = startX + j * sizeBox;
                                int top = startY + (i - 3) * sizeBox;
                                int right = left + sizeBox;
                                int bottom = top + sizeBox;
                                mCanvas.drawRect(left, top, right, bottom, mPaint);
                            }
                        }
                    }else {
                        if (mArea[i][j] == 1) {
                            mPaint.reset();
                            mPaint.setColor(Color.parseColor(COLOR_GREEN));
                            mPaint.setStyle(Paint.Style.FILL);
                            mPaint.setStrokeWidth(2);
                            int left = startX + j * sizeBox;
                            int top = startY + (i - 3) * sizeBox;
                            int right = left + sizeBox;
                            int bottom = top + sizeBox;
                            mCanvas.drawRect(left, top, right, bottom, mPaint);
                        }
                    }
                    mPaint.reset();
                    mPaint.setColor(Color.parseColor(COLOR_WHITE));
                    mPaint.setStyle(Paint.Style.STROKE);
                    mPaint.setStrokeWidth(2);
                    int left = startX + j * sizeBox;
                    int top = startY + (i - 3) * sizeBox;
                    int right = left + sizeBox;
                    int bottom = top + sizeBox;
                    mCanvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
            String text = "游戏积分：" + gameCredits;
            mPaint.reset();
            mPaint.setColor(Color.parseColor(COLOR_WHITE));
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(2);
            mPaint.setTextSize(60);
            float txtWidth = mPaint.measureText(text);
            mCanvas.drawText(text, mSurfaceView.getMeasuredWidth() / 2 - txtWidth / 2, startY - 30, mPaint);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            mSurfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }

    public void start(){
        over = false;
        puase = false;
        shapeTime = 0;
        if (mThread != null && !mThread.isAlive()){
            mThread.start();
        }
    }

    public void stop(){
        over = true;
        if (mThread != null && mThread.isAlive()){
            mThread.interrupt();
        }
    }

    private void clearSurface() {
        try {
            mCanvas = mSurfaceHolder.lockCanvas();
            if (mCanvas != null) {
                mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Builder {
        MainLogicUtil mMainLogicUtil;

        public Builder(SurfaceView surfaceView){
            mMainLogicUtil = new MainLogicUtil();
            mMainLogicUtil.setSurfaceView(surfaceView);
        }

        public Builder setPadding(int padding){
            mMainLogicUtil.setPadding(padding);
            return this;
        }

        public Builder setHandler(Handler handler){
            mMainLogicUtil.setHandler(handler);
            return this;
        }

        public MainLogicUtil build(){
            return mMainLogicUtil;
        }
    }

    private class HolderCallBack implements SurfaceHolder.Callback{
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
        }
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            if (!puase){
                puase = true;
                clearSurface();
            }
        }
    }

    private void createShape(){
        shapeTime = 0;
        curShapeArrive = false;
        curShapeType = mRandom.nextInt(7);
//        curShapeType = 5;
        curShapeTransformation = mRandom.nextInt(4);
        switch (curShapeType){
            case 0://线
                switch (curShapeTransformation){
                    case 0:
                    case 2:
                        curShape[0][0] = 3;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 0;
                        curShape[2][0] = 5;
                        curShape[2][1] = 0;
                        curShape[3][0] = 6;
                        curShape[3][1] = 0;
                        break;
                    case 1:
                    case 3:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 2;
                        curShape[3][0] = 4;
                        curShape[3][1] = 3;
                        break;
                }
                break;
            case 1://L
                switch (curShapeTransformation){
                    case 0:
                        curShape[0][0] = 3;
                        curShape[0][1] = 1;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 5;
                        curShape[2][1] = 1;
                        curShape[3][0] = 5;
                        curShape[3][1] = 0;
                        break;
                    case 1:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 2;
                        curShape[3][0] = 5;
                        curShape[3][1] = 2;
                        break;
                    case 2:
                        curShape[0][0] = 3;
                        curShape[0][1] = 1;
                        curShape[1][0] = 3;
                        curShape[1][1] = 0;
                        curShape[2][0] = 4;
                        curShape[2][1] = 0;
                        curShape[3][0] = 5;
                        curShape[3][1] = 0;
                        break;
                    case 3:
                        curShape[0][0] = 3;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 0;
                        curShape[2][0] = 4;
                        curShape[2][1] = 1;
                        curShape[3][0] = 4;
                        curShape[3][1] = 2;
                        break;
                }
                break;
            case 2://反L
                switch (curShapeTransformation){
                    case 0:
                        curShape[0][0] = 3;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 0;
                        curShape[2][0] = 5;
                        curShape[2][1] = 0;
                        curShape[3][0] = 5;
                        curShape[3][1] = 1;
                        break;
                    case 1:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 2;
                        curShape[3][0] = 3;
                        curShape[3][1] = 2;
                        break;
                    case 2:
                        curShape[0][0] = 3;
                        curShape[0][1] = 0;
                        curShape[1][0] = 3;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 1;
                        curShape[3][0] = 5;
                        curShape[3][1] = 1;
                        break;
                    case 3:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 3;
                        curShape[1][1] = 0;
                        curShape[2][0] = 3;
                        curShape[2][1] = 1;
                        curShape[3][0] = 3;
                        curShape[3][1] = 2;
                        break;
                }
                break;
            case 3://Z
                switch (curShapeTransformation){
                    case 0:
                    case 2:
                        curShape[0][0] = 3;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 0;
                        curShape[2][0] = 4;
                        curShape[2][1] = 1;
                        curShape[3][0] = 5;
                        curShape[3][1] = 1;
                        break;
                    case 1:
                    case 3:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 3;
                        curShape[2][1] = 1;
                        curShape[3][0] = 3;
                        curShape[3][1] = 2;
                        break;
                }
                break;
            case 4://反Z
                switch (curShapeTransformation){
                    case 0:
                    case 2:
                        curShape[0][0] = 3;
                        curShape[0][1] = 1;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 0;
                        curShape[3][0] = 5;
                        curShape[3][1] = 0;
                        break;
                    case 1:
                    case 3:
                        curShape[0][0] = 3;
                        curShape[0][1] = 0;
                        curShape[1][0] = 3;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 1;
                        curShape[3][0] = 4;
                        curShape[3][1] = 2;
                        break;
                }
                break;
            case 5://山
                switch (curShapeTransformation){
                    case 0:
                        curShape[0][0] = 3;
                        curShape[0][1] = 1;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 0;
                        curShape[3][0] = 5;
                        curShape[3][1] = 1;
                        break;
                    case 1:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 2;
                        curShape[3][0] = 5;
                        curShape[3][1] = 1;
                        break;
                    case 2:
                        curShape[0][0] = 3;
                        curShape[0][1] = 1;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 5;
                        curShape[2][1] = 1;
                        curShape[3][0] = 4;
                        curShape[3][1] = 2;
                        break;
                    case 3:
                        curShape[0][0] = 4;
                        curShape[0][1] = 0;
                        curShape[1][0] = 4;
                        curShape[1][1] = 1;
                        curShape[2][0] = 4;
                        curShape[2][1] = 2;
                        curShape[3][0] = 3;
                        curShape[3][1] = 1;
                        break;
                }
                break;
            case 6://田
                curShape[0][0] = 4;
                curShape[0][1] = 0;
                curShape[1][0] = 4;
                curShape[1][1] = 1;
                curShape[2][0] = 5;
                curShape[2][1] = 0;
                curShape[3][0] = 5;
                curShape[3][1] = 1;
                break;
        }
    }

    private boolean collisionJudgment(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
        if (x1 >= 0 && x1 < mArea[0].length && x2 >= 0 && x2 < mArea[0].length &&
                x3 >= 0 && x3 < mArea[0].length && x4 >= 0 && x4 < mArea[0].length &&
                y1 >= 0 && y1 < mArea.length && y2 >= 0 && y2 < mArea.length &&
                y4 >= 0 && y4 < mArea.length && y4 >= 0 && y4 < mArea.length){
            if (mArea[y1][x1] == 0 && mArea[y2][x2] == 0 &&
                    mArea[y3][x3] == 0 && mArea[y4][x4] == 0){
                return true;
            }
        }
        return false;
    }

    public void setOver(boolean over) {
        this.over = over;
    }

    public void setCurShapeFast(boolean curShapeFast) {
        this.curShapeFast = curShapeFast;
    }

    public void setPuase(boolean puase) {
        this.puase = puase;
    }

    public void moveToLeft(){
        if (curShape[0][0] - 1 >=0 && curShape[1][0] - 1 >= 0 &&
                curShape[2][0] - 1 >= 0 && curShape[3][0] - 1 >= 0){
            if (mArea[curShape[0][1]][curShape[0][0] - 1] == 0 &&
                    mArea[curShape[1][1]][curShape[1][0] - 1] == 0 &&
                    mArea[curShape[2][1]][curShape[2][0] - 1] == 0 &&
                    mArea[curShape[3][1]][curShape[3][0] - 1] == 0) {
                curShape[0][0] -= 1;
                curShape[1][0] -= 1;
                curShape[2][0] -= 1;
                curShape[3][0] -= 1;
            }
        }
    }

    public void moveToRight(){
        if (curShape[0][0] + 1 < mArea[0].length && curShape[1][0] + 1 < mArea[0].length &&
                curShape[2][0] + 1 < mArea[0].length && curShape[3][0] + 1 < mArea[0].length){
            if (mArea[curShape[0][1]][curShape[0][0] + 1] == 0 &&
                    mArea[curShape[1][1]][curShape[1][0] + 1] == 0 &&
                    mArea[curShape[2][1]][curShape[2][0] + 1] == 0 &&
                    mArea[curShape[3][1]][curShape[3][0] + 1] == 0) {
                curShape[0][0] += 1;
                curShape[1][0] += 1;
                curShape[2][0] += 1;
                curShape[3][0] += 1;
            }
        }
    }

    public void transformation(){
        int x1 = 0;
        int y1 = 0;
        int x2 = 0;
        int y2 = 0;
        int x3 = 0;
        int y3 = 0;
        int x4 = 0;
        int y4 = 0;
        switch (curShapeType){
            case 0://线
                switch (curShapeTransformation){
                    case 0:
                    case 2:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1];
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 1:
                    case 3:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1];
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3 + 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                }
                break;
            case 1://L
                switch (curShapeTransformation){
                    case 0:
                        x1 = curShape[0][0];
                        y1 = curShape[0][1] - 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 1:
                        x1 = curShape[0][0];
                        y1 = curShape[0][1] + 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3;
                        y4 = y3 - 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 2:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1] - 1;
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3 + 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 3:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1] + 2;
                        x2 = x1;
                        y2 = y1 - 1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3 + 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                }
                break;
            case 2://反L
                switch (curShapeTransformation){
                    case 0:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1];
                        x2 = x1 - 1;
                        y2 = y1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 1:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1] + 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 2:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1] - 1;
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3 - 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 3:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1];
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3 + 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                }
                break;
            case 3://Z
                switch (curShapeTransformation){
                    case 0:
                    case 2:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1] - 1;
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2 - 1;
                        y3 = y2;
                        x4 = x3;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 1:
                    case 3:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1] + 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3 + 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                }
                break;
            case 4://反Z
                switch (curShapeTransformation){
                    case 0:
                    case 2:
                        x1 = curShape[0][0];
                        y1 = curShape[0][1] - 1;
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 1:
                    case 3:
                        x1 = curShape[0][0];
                        y1 = curShape[0][1] + 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2;
                        y3 = y2 - 1;
                        x4 = x3 + 1;
                        y4 = y3;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                }
                break;
            case 5://山
                switch (curShapeTransformation){
                    case 0:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1] - 1;
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3 - 1;
                        y4 = y3 - 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 1:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1] + 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2;
                        y3 = y2 - 1;
                        x4 = x3 + 1;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 2:
                        x1 = curShape[0][0] + 1;
                        y1 = curShape[0][1] - 1;
                        x2 = x1;
                        y2 = y1 + 1;
                        x3 = x2;
                        y3 = y2 + 1;
                        x4 = x3 + 1;
                        y4 = y3 - 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                    case 3:
                        x1 = curShape[0][0] - 1;
                        y1 = curShape[0][1] + 1;
                        x2 = x1 + 1;
                        y2 = y1;
                        x3 = x2 + 1;
                        y3 = y2;
                        x4 = x3 - 1;
                        y4 = y3 + 1;
                        if (collisionJudgment(x1, y1, x2, y2, x3, y3, x4, y4)){
                            curShape[0][0] = x1;
                            curShape[0][1] = y1;
                            curShape[1][0] = x2;
                            curShape[1][1] = y2;
                            curShape[2][0] = x3;
                            curShape[2][1] = y3;
                            curShape[3][0] = x4;
                            curShape[3][1] = y4;
                            curShapeTransformation = (curShapeTransformation + 1) % 4;
                        }
                        break;
                }
                break;
            case 6://田
//                curShape[0][0] = 4;
//                curShape[0][1] = 0;
//                curShape[1][0] = 4;
//                curShape[1][1] = 1;
//                curShape[2][0] = 5;
//                curShape[2][1] = 0;
//                curShape[3][0] = 5;
//                curShape[3][1] = 1;
                break;
        }
    }
}
